package com.aquila.datas;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	private Context context ;
	private TextView textView ;
	Button button ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this ;
		button = (Button) findViewById(R.id.button) ;
		button.setOnClickListener(this) ;
		textView = (TextView) findViewById(R.id.text_01) ;
		
	}
	
	
	@Override
	public void onClick(View v) {
		if (v == button) {
			showConfigDialog() ;
        	
        }
	}
	
	
	
	private ProgressDialog progressDialog ;
	 private void showConfigDialog(){
	    	progressDialog = new ProgressDialog(this) ;
	    	progressDialog.setCancelable(false) ;
	    	progressDialog.setTitle("正在配置程序...") ;
	    	progressDialog.setMessage("正在进行兼容处理，请稍后。") ;
	    	progressDialog.show() ;
	    	new Thread(){
	    		public void run() {
	    			boolean flag = false ;
	    			if (DatabaseTools.isDataExist()) { // 表示存在
	    				flag = DatabaseTools.updataDataBase(context, false) ;
//	    				DatabaseTools.updateLXDataBase(context, false) ;
//	    				DatabaseTools.updateEveryDataBase(context, false);
					}
	    			else { //表示创建
	    				flag =DatabaseTools.updataDataBase(context, true) ;
//	    				DatabaseTools.updateLXDataBase(context, true) ;
//	    				DatabaseTools.updateEveryDataBase(context, true);
					}
	    			if (flag) {
	    				handler.sendEmptyMessage(1) ;
					}
	    			else {
	    				handler.sendEmptyMessage(2) ;
					}
	    		};
	    	}.start() ;
	    }
	
	 Handler handler = new Handler(){
		 public void handleMessage(android.os.Message msg) {
			 if (msg.what == 1) {
				 if (progressDialog != null && progressDialog.isShowing()) {
		    			progressDialog.dismiss() ;
	            	}
				 textView.setText("兼容数据处理完成，请启动圣经客户端，查看是否可以运行，如果圣经程序可以运行，请卸载本程序。如果不能运行，请在程序中反馈告知作者") ;
			}
			 else if (msg.what == 2) {
				 if (progressDialog != null && progressDialog.isShowing()) {
					 progressDialog.dismiss() ;
				 }
				 textView.setText("数据处理失败。") ;
			}
		 };
	 } ;
	
	
	 
	 
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
